<template>
  <link rel="stylesheet" href="/assets/css/coupon-info.css" />

  <div class="headerbox">
    <div class="header">
      <div class="headerL">
        <a onclick="javascript:history.back(-1)" class="goback"
          ><img src="/assets/images/goback.png"
        /></a>
      </div>
      <div class="headerC">
        <p>优惠券详情</p>
      </div>
    </div>
  </div>
  <div class="clear"></div>
  <div class="h50"></div>
  <div class="coupon_detail">
    <div class="coupon_info">
      <div class="left">
        <div class="left_top">
          <div class="rate">
            <span>{{ info.rate * 100 }}</span
            >折
          </div>
          <div class="top_info">
            <div>{{ info.title }}</div>
            <div>COUPON</div>
          </div>
        </div>
        <div class="left_tips">
          订单满<span>100</span>元,可享<span>{{ info.rate * 100 }}</span
          >折
        </div>
      </div>
      <div class="receive">
        <button class="right_btn" @click="Receive" v-if="info.receive">立即领取</button>
        <button class="receive_btn" v-else>已领取</button>
      </div>
    </div>
    <div class="coupon_prompt">
      <div class="prompt_title">温馨提示：</div>
      <div><span>•</span>每人限领1张</div>
      <div><span>•</span>仅剩{{ info.total }}张，赶快领取！</div>
      <div><span>•</span>活动开始时间: {{ info.createtime_text }} 00:00:00</div>
      <div><span>•</span>活动结束时间: {{ info.endtime_text }} 24:00:00</div>
    </div>
  </div>
</template>
<script setup>
import { Empty } from "vant";
import { getCurrentInstance, onBeforeMount, onMounted } from "vue";

const { proxy } = getCurrentInstance();
//当前会员信息
const business = proxy.$cookies.get('business') ? proxy.$cookies.get('business') : {};
console.log(business)
const route = useRoute();
const cid = route.params.cid;
const busid = business.id ? business.id : null;
console.log(busid)
const info = ref([]);

onBeforeMount(async () => {
  var result = await proxy.$POST({
    url: "coupon/info",
    params: { cid, busid },
  });

  if (result.code == 0) {
    proxy.$fail(result.msg);
    return;
  } else if (result.code == 1) {
    info.value = result.data.res;
  }

  console.log(info.value);
});

const Receive = async () => {
  //先判断是否有登录
  var result = await proxy.$login();
  //查询用户失败
  if (result.code == 0) {
    //保留当前的地址
    let url = `/coupon/couponinfo/`+cid
    proxy.$cookies.set("redirect", url);
    console.log(url)  

    //清除缓存
    proxy.$cookies.remove("business");

    proxy
      .$confirm({
        message: "请先前往登录",
      })
      .then(() => {
        //确认
        //删除cookie，跳转
        proxy.$cookies.remove("busineee");
        proxy.$router.push(result.url);
        return
      })
      .catch(()=>{});
      return
  }

  //如果用户存在，返回最新的数据，覆盖cookie
  proxy.$cookies.set("business", result.data);

  var res = await proxy.$POST({
    url:'coupon/receive',
    params:{cid,busid}
  })

  if(res.code == 0){
    proxy.$fail({
      message:res.msg,
      duration:1000,
      onClose:()=>{
        proxy.$router.push(res.url)
      }
    })
    
    return
  }else if(res.code == 1){
    proxy.$success({
      message:res.msg,
      duration:1000,
      onClose:()=>{
        proxy.$router.push(res.url)
      }
    })
    return
  }
};

</script>